import TableSearch from '@/components/tableSearch'
import TableList from '@/components/standardTable'

export default {
    components: {
        TableSearch,
        TableList
    },
    props:{
        group:{//已选
            type: Array,
            default: () => []
        }
    },
    data() {
        return {
            form: {
                page: 1,
                size: 10,
            },
            loading: false,
            selectionBox: [],
            list: [],
            total: 0,
            selectedRowKeys: [],
            scroll: {},
            timeout: null,
            storageRowList:[]
        }
    },
    computed: {
        pagination: function () {
            return { showSizeChanger: true, pageSize: this.form.size, current: this.form.page || 1, total: this.total || 0, onChange: this.pageChange, onShowSizeChange: this.sizeChange }
        },
        selection: function () {
            return { selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }
        }
    },
    watch:{
        group:{
            handler(value){
                if(!Array.isArray(value) || !value.length){
                    this.selectedRowKeys = []
                }else{
                    let keys = [];
                    value.map(item=>{
                        keys.push(item.id)
                    });
                    this.selectedRowKeys = keys;
                }
            },
            deep:true
        }
    },
    mounted() {
        // const handler = () => this.debounce(()=>this.search(1));
        // Object.keys(this.form).filter(item => !["current"].includes(item)).forEach(key => {
        //     this.$watch(vm => vm.form[key], handler, {
        //         deep: true
        //     });
        // });
    },
    created() {
        // this.setScrollWidth();
        // if(!this.standAlone){//是否需要独立调用 开始搜索页面
        //     this.$nextTick(()=>{
        //         this.debounce(()=>this.search(1),100,true)
        //     })
        // }
    },
    methods: {
        reset() {
            for (const key in this.originForm) {
                this.form[key] = this.originForm[key]
            }
            this.debounce(() => this.search(1))
        },
        //每页数量变化
        sizeChange(current, size) {
            this.form.size = size;
            this.search(current)
            if(!this.isConcise){
                this.selectionBox = []
                this.selectedRowKeys = []
            }
        },
        //分页数变化
        pageChange(current) {
            this.search(current)
            if(!this.isConcise){
                this.selectionBox = []
                this.selectedRowKeys = []
            }
        },
        //多选
        onSelectChange(selectedRowKeys, selectedRows,e) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionBox = selectedRows;
            this.storageRowList = this.filterArray([...this.storageRowList,...selectedRows])
            //通过rowkeys查询到用户信息 并发送给上层
            let selectedList = this.getSelectionList(selectedRowKeys,this.storageRowList)
            this.$emit('selection',selectedList)
        },
        //获取已经选择的所有数据
        getSelectionList(keys,list){
            if(!Array.isArray(keys)) return [];
            if(!Array.isArray(list)) return [];
            if(keys.length==0 || list.length==0) return [];
            let data = [];
            keys.map(key=>{
                let getItem = list.find(item=>item.id==key);
                data = [...data,getItem]
            });
            return data;
        },
        //过滤重复
        filterArray(array){
            if(!array) return [];
            if(!Array.isArray(array)) return [];
            let newArray = [];
            array.map(item=>{
                let {id} = item;
                let isFindIndex = newArray.findIndex(find=>find.id==id);
                if(isFindIndex==-1){
                    newArray.push(item)
                }
            })
            return newArray
        },
        //屏幕宽度
        setScrollWidth() {
            let width = document.body.clientWidth;
            let scrollWidth = width - 332;
            console.log(scrollWidth)
            this.scroll = {
                x: 1500
            }
        },
        //监听resize
        initResizeEvent() {
            window.addEventListener('resize', this.setScrollWidth);
        },
        //移除resize
        destroyResizeEvent() {
            window.removeEventListener('resize', this.setScrollWidth);
        },
        debounce(func, wait = 500, immediate = false) {
            // 清除定时器
            if (this.timeout !== null) clearTimeout(this.timeout);
            // 立即执行，此类情况一般用不到
            if (immediate) {
                var callNow = !this.timeout;
                this.timeout = setTimeout(function () {
                    this.timeout = null;
                }, wait);
                if (callNow) typeof func === 'function' && func();
            } else {
                // 设置定时器，当最后一次操作后，timeout不会再被清除，所以在延时wait毫秒后执行func回调方法
                this.timeout = setTimeout(function () {
                    typeof func === 'function' && func();
                }, wait);
            }
        }
    },
}
